﻿using System.Data;
using System.Data.Common;
using System.Data.Objects;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.ComponentModel;
using System;
using System.Reflection;
using System.Text;

namespace AstekServis.Web.Data
{
    public static class Methods
    {
        public static object ExecuteSql(ObjectContext c, string sql)
        {
            object sonuc = null;
            var entityConnection = (System.Data.EntityClient.EntityConnection)c.Connection;
            DbConnection conn = entityConnection.StoreConnection;
            ConnectionState initialState = conn.State;
            try
            {
                if (initialState != ConnectionState.Open)
                    conn.Open();  // open connection if not already open
                using (DbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    sonuc = cmd.ExecuteScalar();
                }
            }
            finally
            {
                if (initialState != ConnectionState.Open)
                    conn.Close(); // only close connection if not initially open
            }
            return sonuc;
        }
        public static string ToStr(this DateTime value)
        {
            return value.Day.ToString("00") + "." + value.Month.ToString("00") + "." + value.Year.ToString();
        }
        public static string SolaYasla(this string value, int lenght)
        {
            if (value.Length < lenght)
            {
                return value.PadRight(lenght, ' ');
            }
            else
            {
                return value.Substring(0, lenght);
            }
        }
        public static string SolaYasla(this DateTime? value, int lenght)
        {
            string strDate = "";
            if (value.HasValue)
                strDate = value.Value.Day + "/" + value.Value.Month + "/" + value.Value.Year;

            if (strDate.Length < lenght)
            {
                return strDate.PadRight(lenght, ' ');
            }
            else
            {
                return strDate.Substring(0, lenght);
            }

        }
        public static string SagaYasla(this string value, int lenght)
        {
            if (value.Length < lenght)
            {
                return value.PadLeft(lenght, ' ');
            }
            else
            {
                return value.Substring(0, lenght);
            }
        }
        public static string SagaYasla(this double? value, int lenght)
        {
            string strSayi = "";
            if (value.HasValue)
                strSayi = value.Value.ToString("n2");

            if (strSayi.Length < lenght)
            {
                return strSayi.PadLeft(lenght, ' ');
            }
            else
            {
                return strSayi.Substring(0, lenght);
            }
        }
        public static string Yerlestir(this string value, int index, string deger)
        {
            string sonuc = "";
            if (value.Length >= index + deger.Length)
            {
                sonuc += value.Substring(0, index);
                sonuc += deger;
                sonuc += value.Substring(index + deger.Length);
            }
            else
            {
                sonuc = value;
            }
            return sonuc.Replace('İ', 'I').Replace('Ö', 'O').Replace('Ü', 'U').Replace('Ş', 'S').Replace('Ç', 'C').Replace('ı', 'i').Replace('ö', 'o').Replace('ü', 'u').Replace('ş', 's').Replace('ç', 'c');
        }
        public static string TarihDuzelt(this string value)
        {
            string sonuc = "";
            if (!string.IsNullOrEmpty(value))
            {
                string[] pr1 = value.Trim().Split(' ')[0].Split('-');
                sonuc = string.Format("{0}.{1}.{2} {3}", pr1[2], pr1[1], pr1[0], value.Trim().Split(' ')[1]);
            }
            return sonuc;
        }
    }
}